﻿package view.components.background 
{
	import br.com.stimuli.loading.BulkLoader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import model.utils.ModuleConfig;
	import org.casalib.display.CasaMovieClip;
	import view.components.IMainComponent;
	import view.components.background.BackgroundStrip;
	
	import org.casalib.util.AlignUtil;
	
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author Lobanov
	 */
	public class BackgroundSlideshow extends Sprite implements IMainComponent
	{
		private var _imageContainer:CasaMovieClip;
		private var _slicesContainer:CasaMovieClip;
		private var _bgColor:Sprite;
		
		private var _imgArr:Array;
		private var _bulkLoader:BulkLoader;
		
		private var _iterator:int = 0;
		
		private var _previousImg:Bitmap;
        private var _img:Bitmap = null;
		
		public function BackgroundSlideshow($xmlData:XML) 
		{
			_imgArr = [];
			
			for each (var img:XML in $xmlData.mainslideshow.image) {
				_imgArr.push( { link: ModuleConfig.DOMAIN + img.@link } );
			}
			
			_imageContainer = new CasaMovieClip();
			_imageContainer.alpha = 1;
			_slicesContainer = new CasaMovieClip();
			_slicesContainer.alpha = 1;
			
			_previousImg = new Bitmap(new FirstImage(0, 0));
			_imageContainer.addChildAt(_previousImg, 0);
			
			_bgColor = new Sprite();
			_bgColor.graphics.beginFill(0x000000, 1);
			_bgColor.graphics.drawRect(0, 0, 1, 1);
			_bgColor.graphics.endFill();
			
			this.addChild(_bgColor);
			this.addChild(_imageContainer);
			this.addChild(_slicesContainer);
			
			//show first image
			nextImageLoader(_imgArr[iterator].link);
		}
		
		/**
		 * Next image liader
		 * @param	$link
		 */
		private function nextImageLoader($link:String):void {
			_bulkLoader = new BulkLoader("image loader");
			
			_bulkLoader.add($link, { id:"image" } );
			_bulkLoader.addEventListener(BulkLoader.COMPLETE, onCompleteHandler);
			_bulkLoader.addEventListener(BulkLoader.PROGRESS, onProgressHandler);
			_bulkLoader.start();
			
			function onProgressHandler(evt:ProgressEvent):void {
				//trace("Loaded" , evt.bytesLoaded," of ",  evt.bytesTotal);
			}
			
			function onCompleteHandler(evt:ProgressEvent):void {		
				showNextImage(new Bitmap(_bulkLoader.getBitmapData("image", true) as BitmapData));
				_bulkLoader.clear();
				removeEventListener(BulkLoader.COMPLETE, onCompleteHandler);
				removeEventListener(BulkLoader.PROGRESS, onProgressHandler);
			}
		}
		
		/**
		 * show next images
		 * 
		 * _bgColor use for getting width and height of stage :)
		 * @param	$bm
		 */
		private function showNextImage($bm:Bitmap):void {
			$bm.smoothing = true;
			_imageContainer.removeChildren(true);
			_imageContainer.addChild($bm);
			
			var bgSprite:BackgroundStrip = new BackgroundStrip(new Rectangle(0, 0, 1280,  1024), 1, 
											Bitmap(_previousImg).bitmapData );
											
			_slicesContainer.addChild(bgSprite);
			stripePosition();
			
			_previousImg = $bm;
			
			clickTimer = new Timer(10000, 1);
			clickTimer.addEventListener("timer", timerClickHandler);
			clickTimer.start();
			
			function timerClickHandler(event:TimerEvent):void {
				//nextImageLoader(_imgArr[iterator].link);
				if (bgSprite) {
					bgSprite.removeChildren(true);
					_slicesContainer.removeChildren(true);
				}
			}
			
			//request first stage size
			ApplicationFacade.getInstance().sendNotification( ApplicationFacade.DISPATCH_STAGE_RESIZE, null );
		}
		
		public function setColorBg($color:int = 0x000000):void 
		{
			TweenLite.to(_bgColor, 1, { tint:$color } );
		}
		
		public function setPosition($width:Number, $height:Number):void 
		{
			positionMovie(_imageContainer);
			stripePosition();
			
			_bgColor.width = $width;
			_bgColor.height = $height;
		}
		
		private function positionMovie(mc:CasaMovieClip):void {
			mc.scaleY = mc.scaleX = 100;
           
            var ratio_x:uint = stage.stageWidth/mc.width;
            var ratio_y: uint = stage.stageHeight/mc.height;
            if (ratio_x<=ratio_y) {
                if (ratio_x < 1) {
                    mc.width = stage.stageWidth;
                    mc.scaleY = mc.scaleX;
                }
            } else {
                if (ratio_y < 1) {
                    mc.height = stage.stageHeight;
                    mc.scaleX = mc.scaleY;
                }
            }
           
			
			if ( stage.stageWidth <= 1024) {
				mc.width = 1024;
				mc.height = 819;
			} else {
				mc.x = Math.round((stage.stageWidth - mc.width) / 2);
				mc.y = Math.round((stage.stageHeight - mc.height) / 2);  
			}
		}
		
		private function stripePosition():void {
			AlignUtil.alignTop(_slicesContainer, new Rectangle(0,0, stage.stageWidth, stage.stageHeight));
			_slicesContainer.scaleY = _imageContainer.scaleX;
			_slicesContainer.scaleX = _imageContainer.scaleY;
		}
		
		private function get iterator():int {
			_iterator = (_iterator >=  _imgArr.length -1) ? 0 : ++_iterator;
			return _iterator;
		}
		
	}

}